package com.mindskip.xzs.controller.student;

import com.mindskip.xzs.configuration.spring.cache.CacheConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/Zd")
public class ZdTestController {

    private final RedisTemplate<String, Object> redisTemplate;
    private final static String CACHE_NAME = "zd";
    private final CacheConfig cacheConfig;
    @Autowired
    public ZdTestController(RedisTemplate<String, Object> redisTemplate, CacheConfig cacheConfig) {
        this.redisTemplate = redisTemplate;
        this.cacheConfig = cacheConfig;
    }

    @GetMapping("/Test")
    public String zdTest(){
        System.out.println("权限测试");
        return "RestResponse.ok()";
    }


    @GetMapping("/addCache")
    public String addCache(String id){
        String key = cacheConfig.simpleKeyGenerator(CACHE_NAME, id);
        redisTemplate.opsForValue().set(key, "I Love ZM " + id );
        System.out.println("\"Token缓存已经设置好\" = " + "Token缓存已经设置好");
        return "I Love ZM " + id;
    }

    @GetMapping("/getCache")
    @Cacheable(value = CACHE_NAME, key = "#id", unless = "#result == null")
    public String getCache(String id){
       return "Cache False";
    }

    @GetMapping("/deleteCache")
    public String deleteCache(String id){
        String key = cacheConfig.simpleKeyGenerator(CACHE_NAME, id);
        redisTemplate.delete(key);
        return "delete Suceess";
    }


}
